import { Module, DynamicModule, Global } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { ZsyzTokenService, ZsyzTokenConfig } from './modules/auth/zsyz-token.service';
import { ZsyzTokenGuard } from './modules/auth/zsyz-token.guard';
import { LicenseInterceptor } from './modules/auth/license.interceptor';
import { StartupValidationModule } from './modules/decrypt/startup-validation.module';

export interface ZsyzModuleOptions {
    secret: string;
    expiresIn?: string;
    isGlobal?: boolean;
}

@Module({})
export class ZsyzModule {
    /**
     * 注册 ZsyzModule
     * @param options - 配置选项
     * @returns DynamicModule
     */
    static register(options: ZsyzModuleOptions): DynamicModule {
        const config: ZsyzTokenConfig = {
            secret: options.secret,
            expiresIn: options.expiresIn || '7d'
        };

        // 基础 providers
        const providers: any[] = [
            {
                provide: 'ZSYZ_JWT_CONFIG',
                useValue: config
            },
            ZsyzTokenService,
            ZsyzTokenGuard
        ];

        // 模块导入
        const imports: any[] = [];

        // 模块导出
        const exports: any[] = [ZsyzTokenService, ZsyzTokenGuard];

        // 启用运行时许可证检查（默认开启）
        providers.push({
            provide: APP_INTERCEPTOR,
            useClass: LicenseInterceptor,
        });

        // 启用启动验证（默认开启）
        imports.push(StartupValidationModule);

        return {
            module: ZsyzModule,
            imports,
            providers,
            exports,
            global: options.isGlobal || false,
        };
    }
} 